package com.natalia.file.struct;

import com.natalia.file.entity.*;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author yang
 **/
@Mapper(componentModel = "spring")
public interface UploadStructBasic {

    UploadStructBasic INSTANCES = Mappers.getMapper(UploadStructBasic.class);


    /**
     *  实体转换
     * @return UploadResult
     */
    @Mappings(value = {
            @Mapping(source = "fileName", target = "fileName"),
            @Mapping(source = "fileExt", target = "fileExt"),
            @Mapping(source = "fileSize", target = "fileSize")
    })
    UploadResult toUploadResult(UploadInfo uploadInfo);

    /**
     * 实体转换
     * @param multipartUploadInfo
     * @return
     */
    @Mappings(value = {
            @Mapping(source = "uploadId", target = "uploadId"),
            @Mapping(source = "fileName", target = "fileName"),
            @Mapping(source = "partNum", target = "partNum")
    })
    UploadInfo  multipartToUploadInfo(MultipartUploadInfo multipartUploadInfo);

    @Mappings(value = {
            @Mapping(source = "fileName", target = "fileName"),
            @Mapping(source = "uploadId", target = "uploadId"),
            @Mapping(source = "partNumber", target = "partNumber"),
            @Mapping(source = "file", target = "file")
    })
    UploadPartInfo uploadPartReqToPartInfo(UploadPartRequest uploadPartRequest);


    @Mappings(value = {
            @Mapping(source = "fileName", target = "objectName"),
            @Mapping(source = "uploadId", target = "uploadId"),
            @Mapping(source = "maxParts", target = "maxParts")
    })
    MultipartUploadCreate listPartsReqToMultipartUploadCreat(ListPartsRequest listPartsRequest);

    @Mappings(value = {
            @Mapping(source = "partId", target = "partId"),
            @Mapping(source = "uploadId", target = "uploadId"),
            @Mapping(source = "partNumber", target = "partNumber"),
            @Mapping(source = "fileStatus", target = "fileStatus")
    })
    ShardUploadInfo uploadPartToShardUploadInfo(UploadPartInfo uploadPartInfo);


    @Mappings(value = {
            @Mapping(source = "uploadId", target = "uploadId"),
            @Mapping(source = "fileName", target = "fileName"),
            @Mapping(source = "chunks", target = "chunks")
    })
    CompleteMultipart completeMultipartReqToComplete(CompleteMultipartRequest completeMultipartRequest);

    /**
     * 集合复制
     * @param sourceItems
     * @return
     */
    default List<ShardUploadInfo> uploadPartToShardUploadInfos(List<UploadPartInfo> sourceItems) {
        return sourceItems.stream()
                .map(this::uploadPartToShardUploadInfo)
                .collect(Collectors.toList());
    }
}
